package server;

import shared.Packet;
import shared.Packet0Connect;
import shared.Packet1Chat;
import shared.Packet2Userlist;
import shared.Packet3StyleChat;
import shared.Packet4Peer;
import shared.Packet5InvalidName;

public class PacketHandler {
    TestingServer host;

    public PacketHandler(TestingServer host){
        this.host = host;
    }

    public void handlePacket(ClientConnection from, Packet packet){
        if(packet.id == 0) handlePacket0Connect(from, (Packet0Connect)packet);
        else if(!from.isConnected) return;
        else if(packet.id == 1) handlePacket1Chat(from, (Packet1Chat)packet);
        else if(packet.id == 2) return;
        else if(packet.id == 3) handlePacket3StyleChat(from, (Packet3StyleChat)packet);
        else if(packet.id == 4) handlePacket4Peer(from, (Packet4Peer)packet);
        else if(packet.id == 5) handlePacket5InvalidName(from, (Packet5InvalidName)packet);
    }

    public void handlePacket0Connect(ClientConnection from, Packet0Connect packet){
    	if(host.connectionManager.doesNameExist(packet.name)){
    		from.sendPacket(new Packet5InvalidName());
    		host.serverGUI.addLine("Invalid name from " + from.connection.getInetAddress().getHostName());
    		return;
    	}
        from.name = packet.name;
        String message = from.name + " connected to the server.";
        host.serverGUI.addLine(message);
        host.packetHandler.sendGlobalPacket(new Packet2Userlist(host.connectionManager.getNames()));
        host.serverGUI.updateUserList(host.connectionManager.getNames());
        from.isConnected = true;
        sendGlobalPacketBut(from, new Packet1Chat("Network: " + message));
    }

    public void handlePacket1Chat(ClientConnection from, Packet1Chat packet){
    	host.serverGUI.write(from.name+": ", from.style);
        host.serverGUI.addLine(packet.message);
        sendGlobalPacketBut(from, new Packet3StyleChat(from.name, packet.message, from.style));
    }
    
    public void handlePacket2Userlist(ClientConnection from, Packet2Userlist packet){
    	// Nothing
    }
    
    public void handlePacket3StyleChat(ClientConnection from, Packet3StyleChat packet){
        host.serverGUI.write(from.name+": ", from.style);
        host.serverGUI.addLine(packet.message);
        sendGlobalPacketBut(from, new Packet3StyleChat(from.name, packet.message, from.style));
    }
    
    public void handlePacket4Peer(ClientConnection from, Packet4Peer packet){
    	// Nothing
    }
    
    public void handlePacket5InvalidName(ClientConnection from, Packet5InvalidName packet){
    	// Nothing
    }

    public void sendGlobalPacket(Packet packet){
        for(int i = 0; i < host.connectionManager.connections.size(); i++)
            host.connectionManager.connections.get(i).sendPacket(packet);
    }

    public void sendGlobalPacketBut(ClientConnection skip, Packet packet){
        for(int i = 0; i < host.connectionManager.connections.size(); i++)
            if(!host.connectionManager.connections.get(i).equals(skip))
                host.connectionManager.connections.get(i).sendPacket(packet);
    }
}
